package com.huanggf.meituan.login;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.huanggf.meituan.MainActivity;
import com.huanggf.meituan.R;
import com.huanggf.meituan.tabhost.MainTabHostActivity;
import com.huanggf.meituan.utils.DateFormatUtil;

import java.util.Calendar;
import java.util.Date;

public class LoginActivity extends AppCompatActivity {

    private ImageView quit;
    private TextView help;
    private EditText phone;
    private EditText password;
    private Button login;
    private TextView verfify_login;
    private TextView enquery;
    private ImageView wechat;
    private ImageView qq;
    private SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        quit= (ImageView) findViewById(R.id.quit);
        help= (TextView) findViewById(R.id.help);
        phone= (EditText) findViewById(R.id.phone);
        password= (EditText) findViewById(R.id.password);
        login= (Button) findViewById(R.id.login);
        verfify_login= (TextView) findViewById(R.id.verfify_login);
        enquery= (TextView) findViewById(R.id.enquery);
        wechat= (ImageView) findViewById(R.id.wechat);
        qq= (ImageView) findViewById(R.id.qq);

        sqLiteDatabase= SQLiteDatabase.openOrCreateDatabase("/data/data/com.huanggf.meituan/databases/meituan.db",null);

        //登录功能
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "SELECT count(*) FROM login WHERE username=?";
                Cursor c1 = sqLiteDatabase.rawQuery(sql,new String[]{phone.getText().toString().trim()});
                if(c1.moveToFirst()){
                    if(c1.getInt(0) > 0){
                        String sel = "SELECT count(*) FROM login WHERE username=? AND password=?";
                        String[] option = new String[2];
                        option[0] = phone.getText().toString().trim();
                        option[1] = password.getText().toString().trim();
                        Cursor c = sqLiteDatabase.rawQuery(sel,option);
                        if(c.moveToFirst()){
                            if(c.getInt(0) > 0){
                                //跳转主页
                                Intent i = new Intent(LoginActivity.this, MainTabHostActivity.class);
                                startActivity(i);
                            }else{
                                Toast.makeText(LoginActivity.this,"用户名或密码错误！", Toast.LENGTH_LONG).show();
                            }
                        }
                    }else{
                        //添加数据
                        ContentValues cv = new ContentValues();
                        cv.put("username",phone.getText().toString().trim());
                        cv.put("password",password.getText().toString().trim());
                        cv.put("flag",1);
                        Calendar c = Calendar.getInstance();
                        c.add(Calendar.MONTH,1);
                        cv.put("expire", DateFormatUtil.dateToStr(new Date(c.getTimeInMillis()),DateFormatUtil.YMD));
                        sqLiteDatabase.insert("login",null,cv);
                        //跳转主页
                        Intent i = new Intent(LoginActivity.this,MainTabHostActivity.class);
                        startActivity(i);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(sqLiteDatabase!=null && sqLiteDatabase.isOpen()){
            sqLiteDatabase.close();
        }
    }
}
